package logic

import (
	"context"
	"shoppingcart-srv/shoppingcart"
	"strconv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveAllCartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRemoveAllCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveAllCartLogic {
	return &RemoveAllCartLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RemoveAllCartLogic) RemoveAllCart(req *types.RemoveAllCartRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	value := l.ctx.Value("userId").(string)
	userId, _ := strconv.Atoi(value)
	all, err := l.svcCtx.ShoppingCartSrv.RemoveCartAll(l.ctx, &shoppingcart.RemoveCartAllRequest{UserId: int64(userId)})
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	if !all.Success {
		return &types.Response{
			Code:    -2,
			Message: "清空购物车失败",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    0,
		Message: "清空购物车成功",
		Data:    nil,
	}, nil
}
